using ImageCSharp.Core.Interfaces;
using Microsoft.Win32;
using System.Windows;

namespace ImageCSharp.Views
{
    /// <summary>
    /// 批处理设置窗口
    /// </summary>
    public partial class BatchSettingsWindow : Window
    {
        public BatchProcessingConfiguration Configuration { get; private set; } = new();
        private readonly IPluginService _pluginService;

        public BatchSettingsWindow(IPluginService pluginService)
        {
            InitializeComponent();
            _pluginService = pluginService;
            LoadFilters();
        }

        private void LoadFilters()
        {
            var filters = _pluginService.GetFilterPlugins();
            FilterListBox.ItemsSource = filters.Select(f => new FilterSelectionItem 
            { 
                Plugin = f, 
                Name = f.Name,
                IsSelected = false 
            }).ToList();
        }

        private void BrowseInputButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFolderDialog();
            dialog.Title = "选择输入目录";

            if (dialog.ShowDialog() == true)
            {
                InputDirectoryTextBox.Text = dialog.FolderName;
            }
        }

        private void BrowseOutputButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFolderDialog();
            dialog.Title = "选择输出目录";

            if (dialog.ShowDialog() == true)
            {
                OutputDirectoryTextBox.Text = dialog.FolderName;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateInput())
            {
                // 保存配置
                Configuration.InputDirectory = InputDirectoryTextBox.Text;
                Configuration.OutputDirectory = OutputDirectoryTextBox.Text;
                Configuration.ResizeImages = ResizeCheckBox.IsChecked ?? false;
                Configuration.TargetWidth = int.Parse(WidthTextBox.Text);
                Configuration.TargetHeight = int.Parse(HeightTextBox.Text);
                Configuration.ConvertFormat = ConvertFormatCheckBox.IsChecked ?? false;
                Configuration.TargetFormat = ((System.Windows.Controls.ComboBoxItem)FormatComboBox.SelectedItem)?.Content.ToString()?.ToLower() ?? "png";
                Configuration.OverwriteExisting = OverwriteCheckBox.IsChecked ?? false;
                Configuration.MaxDegreeOfParallelism = (int)ParallelismSlider.Value;

                // 获取选中的滤镜
                var selectedFilters = ((List<FilterSelectionItem>)FilterListBox.ItemsSource)
                    .Where(item => item.IsSelected)
                    .Select(item => item.Plugin)
                    .ToList();

                Configuration.Plugins.AddRange(selectedFilters);

                DialogResult = true;
                Close();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private bool ValidateInput()
        {
            if (string.IsNullOrEmpty(InputDirectoryTextBox.Text))
            {
                System.Windows.MessageBox.Show("请选择输入目录", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            if (string.IsNullOrEmpty(OutputDirectoryTextBox.Text))
            {
                System.Windows.MessageBox.Show("请选择输出目录", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            if (ResizeCheckBox.IsChecked == true)
            {
                if (!int.TryParse(WidthTextBox.Text, out int width) || width <= 0)
                {
                    System.Windows.MessageBox.Show("请输入有效的宽度", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                if (!int.TryParse(HeightTextBox.Text, out int height) || height <= 0)
                {
                    System.Windows.MessageBox.Show("请输入有效的高度", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }
            }

            return true;
        }
    }

    public class FilterSelectionItem
    {
        public IFilterPlugin Plugin { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }
}